
package hejie.admin.action;

import hejie.admin.service.EnterpriseTypeService;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionSupport;

import publics.model.EnterpriseType;
import publics.util.MyUtils;
import publics.util.Page;

public class EnterpriseTypeAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	EnterpriseTypeService enterpriseTypeService;  //spring注入进来的 enterpriseTypeService
	private boolean success;   //此参数为必须的， extjs根据这个参数的值判断是否成功执行操作
	private EnterpriseType etype;
	private Page page;   //这是分页查询用的辅助类，用此类可以解决分页查询中要返回多个参数的问题，推荐使用
	private Integer id;
	private String conditions;//查询条件（即搜索页面上文本框中输入的值），多个条件用空格分开
	private int start;   //分页查询的 要查询数据的开始号 ，和offset值相同
	private int limit;  // 每页多少个记录

	public EnterpriseTypeService getEnterpriseTypeService() {
		return enterpriseTypeService;
	}
	@Resource(name="enterpriseTypeService")
	public void setEnterpriseTypeService(EnterpriseTypeService enterpriseTypeService) {
		this.enterpriseTypeService = enterpriseTypeService;
		System.out.println("EnterpriseTypeServiceImlp inject in EnterpriseTypeAction!");
	}

	/**
	 * 添加企业性质
	 * @return
	 */
	public String saveEnterpriseType() {
		System.out.println("已经进入 Action 的 saveEnterpriseType方法！");//测试用，可去掉
		id = (Integer) enterpriseTypeService.saveEnterpriseType(etype);
		if (id != null) {
			success = true;
		}else{
			success = false;  //失败返回
		}
		System.out.println("准备返回，success="+success);
		return SUCCESS;
	}

	/**
	 * 根据条件分页查找企业性质
	 * @return
	 */
	public String findAllEnterpriseType() {  
		List<String> conditionList = new ArrayList<String>();   //模糊查询使用
		MyUtils.addToCollection(conditionList, MyUtils.split(conditions, " ,"));//模糊查询使用
		page = new Page(); // 实例化分页对象
		page.setConditions(conditionList);// 设置查询条件
		page.setStart(start);
		page.setLimit(limit = (limit == 0) ? 500 : limit);
		page = enterpriseTypeService.findByPage(page);  //查询 
		if ((null != page.getRoot() && page.getTotalProperty() > 0 )||(null == page.getRoot() && page.getTotalProperty() == 0 )) {
			page.setSuccess(true);
		}else{
			page.setSuccess(false);//查找失败
		}
		return SUCCESS;
	}

	/**
	 * 删除企业性质
	 * @return
	 */
	public String deleteEnterpriseType() {
		if(Integer.valueOf(id) < 1){
			System.out.println("修改失败！企业性质id为="+etype.getId());
			success = false;
		}else{
		System.out.println("enterpriseTypeAction delete id="+id);	
		success = enterpriseTypeService.deleteEnterpriseType(Integer.valueOf(id));
		}
		return SUCCESS;
	}
	
	/**
	 * 修改企业性质指定字段的值
	 * @return
	 * @throws Exception
	 */
	public String updateEnterpriseType() throws Exception {
		
		if(Integer.valueOf(etype.getId()) < 1){
			System.out.println("修改失败！ 企业性质id为="+etype.getId());
			success = false;
		}else{
			success = enterpriseTypeService.updateEnterpriseType(etype);
		}
		return SUCCESS;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public EnterpriseType getEnterpriseType() {
		return etype;
	}

	public void setEnterpriseType(EnterpriseType etype) {
		this.etype = etype;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}

}
